//
//  NineViewController.m
//  UI12豆瓣大项目
//
//  Created by 李奇 on 15/10/31.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//
#define WIDTH  [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height

#import "NineViewController.h"
#import "AppTools.h"
#import "UIImageView+WebCache.h"
#import "FilmDetailViewController.h"
@interface NineViewController ()

@end

@implementation NineViewController
- (void)dealloc
{
    [_title1 release];
    [_pictureV release];
    [_filmArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //加载菊花
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"请耐心等待....";
    //请求网络
    [AppTools getDataWithkey:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) {
        //        NSLog(@"%@",result);
        NSMutableDictionary *bigDic = result;
        NSString *error_code = [bigDic objectForKey:@"error_code"];
        NSString *reason = [bigDic objectForKey:@"reason"];
        NSMutableArray *array = [bigDic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            self.filmArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in array) {
                Activity *activity = [[Activity alloc] init];
                [activity setValuesForKeysWithDictionary:dic];
                [self.filmArray addObject:activity];
                [activity release];
                [self.hub hide:YES];
            }
        }else{
            NSLog(@"请求失败");
        }
        
        //滑动页面
        self.scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
        self.scrollV.contentSize = CGSizeMake(WIDTH, self.filmArray.count/9.0 *(HEIGHT + 40));
        
        //计算图片间隙
        CGFloat intervalwith = (WIDTH - 100 * 3) / 4;
        CGFloat intervalheight = (HEIGHT -165 * 3) /4;
        //铺界面
        for(int i = 0 ; i < self.filmArray.count ; i++){
            CGFloat x = (i % 3) * (100 + intervalwith) + intervalwith;
            CGFloat y = (i / 3) * (165 + intervalheight) + intervalheight;
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 100, 195)];
            view.tag = 1000 + i;
            Activity *activity = [self.filmArray objectAtIndex:i];
            
            self.pictureV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 165)];
            [self.pictureV sd_setImageWithURL:[NSURL URLWithString:activity.pic_url] placeholderImage:[UIImage imageNamed:@"picholder"]];
            [view addSubview:self.pictureV];   //pic_url属性多了 不是对应接口的
            
            self.title1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 165 + 6 , 100, 20)];
            self.title1.text = activity.movieName;
            self.title1.userInteractionEnabled = YES;
            self.title1.font = [UIFont systemFontOfSize:12];
            //居中
            self.title1.textAlignment = NSTextAlignmentCenter;
            
            [view addSubview:self.title1];
            [self.scrollV addSubview:view];
            [self.view addSubview:self.scrollV];
            
            [view release];
            [_pictureV release];
            [_title1 release];
            
            //创建触摸事件
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [view addGestureRecognizer:tap];
            [tap release];
            
        }

        
        
    }];

    
    //导航栏不透明
    self.navigationController.navigationBar.translucent = NO;
//    //右侧按钮
//    UIImage *rightimage = [UIImage imageNamed:@"btn_nav_list@2x"];
//    rightimage = [rightimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightimage style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];
    //左键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    //题目
    self.navigationItem.title = @"电影";
    
    
    
    
}
//右侧按钮
//-(void)rightBarAction:(UIBarButtonItem *)barAction{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//}
//触摸事件
-(void)tapAction:(UITapGestureRecognizer *)tap{
    FilmDetailViewController *detaiVC = [[FilmDetailViewController alloc] init];
    Activity *activity = [self.filmArray objectAtIndex:(tap.view.tag - 1000)];
    detaiVC.movieID = activity.movieId;  //看看上两个文件怎么衔接的就好了
    [self.navigationController pushViewController:detaiVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
